<?php

$index_php = isset($_GET['index'])?$_GET['index']:'admin';
$app = isset($_GET['dir'])?$_GET['dir']:'Admin';
$baseurl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $baseurl."/{$index_php}.php/";


$path = dirname(__FILE__).'/app/'.$app.'/controllers';
$main_list = init($path);
$module_list = array(
	array(
		"name"=>"主模块",
		"list"=>$main_list?$main_list:array()
	)
);


$module__path = dirname(__FILE__).'/module';
$ls_modules = get_module_list($module__path);


foreach($ls_modules as $key=>$val){
	$path = $val['path']."/".$app.'/controllers';
	$ls_list = init($path,$val['module_name']);
	
	$module_list[] = array(
		"name"=>$val['name'],
		"list"=>$ls_list?$ls_list:array()
	);
}



$count = 0;
foreach($module_list as $val){
	foreach($val['list'] as $v){
		$count += count($v['list']);
	}
}
?>
<html>
<head>
	<meta charset="UTF-8">
	<title>Api文档</title>
	<script src="./images/main.js"></script>
<style>
html,body{
	margin:0px;padding:0px;
	color: #333;
}
table,tr,td{
	
}
th,td{border: 1px #bbb solid;padding:  7px;}
table{border-collapse:collapse;border:1px #ccc solid;}
th{
	background:#eee;
}
.nav_box{
    position: fixed;
    right: 30px;
    top: 150px;
    padding: 20px;
    background: #fff;
    border: 1px solid #eee;
	overflow-y: scroll;
    max-height: 600px;
}
.nav_box ul{
    padding-left: 30px;
}
.nav_box ul li{
    margin-bottom: 9px;
}
.nav_box ul li a{
    text-decoration: none;
    color: #333;
}
.content{}


.line_box{
	padding: 20px 30px;
}
.line_box .line_tit{
    float: left;
    width: 150px;
    text-align: right;
    margin-right: 10px;
}
.line_box .line_input{}
.line_box input{
	border: 1px solid #ccc;
    height: 30px;
    line-height: 30px;
    padding: 3px 5px;
    box-sizing: border-box;
}
.line_box .line_input input{
   
}
.token_box{
	position: fixed;
    left: 0;
    width: 100%;
    background: #fff;
    border-bottom: 1px solid #eee;
}
</style>
</head>

<body style="background: url(http://pic1.sc.chinaz.coms/files/pic/pic9/201801/bpic5184.jpg) no-repeat;background-size: 100%;">

<div class="nav_box">
	<?php 
		foreach($module_list as $iii=>$val):
	?>
	<h3><?php echo $val['name']?></h3>
	<ul>
		<?php foreach($val['list'] as $key=>$row):?>
		<li><a href="#nav_<?php echo ($iii+1);?>.<?php echo $key;?>"><?php echo ($iii+1);?>.<?php echo ($key+1);?> <?php echo $row['name'];?>(<?php echo count($row['list']);?>)</a></li>
		<?php endforeach;?>
	</ul>
	<?php endforeach;?>
</div>

<div class="content">
<div class="token_box">
	<div class="line_box">
		<div class="line_tit">
			用户凭证：
			<input type="text" style="width:50px;" value="_token" id="field">
		</div>
		
		<div class="line_input">
			<input type="text" style="width:600px;" id="value" value="">
		</div>
	</div>
</div>
<div style="height: 150px;"></div>

<div class="">
	一共有<span><?php echo $count;?></span>个接口
</div>


<?php 
	foreach($module_list as $iii=>$module):
?>
<h1><?php echo $module['name']?></h1>
	
<?php 
	foreach($module['list'] as $key=>$row):
?>
<div id="nav_<?php echo ($iii+1);?>.<?php echo $key;?>"></div>
<h3><?php echo ($iii+1);?>.<?php echo ($key+1);?> <?php echo $row['name'];?></h3>
<div class="box1" style="margin-left: 50px;">
<?php 
	foreach($row['list'] as $k=>$val):
	$id = ($iii+1) .'.'.($key+1).'.'.($k+1);
?>
<h4><?php echo ($iii+1);?>.<?php echo ($key+1);?>.<?php echo ($k+1);?> <?php echo isset($val['name'])?$val['name']:'未编辑';?></h4>
<div>
	<?php echo ($iii+1);?>.<?php echo ($key+1);?>方法名:  <?php echo $url.$val['func_name'];?>
</div>
<div>
	<p><?php echo ($iii+1);?>.2请求参数</p>
	<table border="1">
		<tr>
			<th>字段名称</th>
			<th>是否必填</th>
			<th>类型及范围</th>
			<th>说明</th>
			<th>自定义值</th>
		</tr>
		<?php if(isset($val['param'])):
			foreach($val['param'] as $params):
		?>
		<tr>
			<td><?php echo isset($params[0])?$params[0]:'';?></td>
			<td><?php echo isset($params[1])?$params[1]:'';?></td>
			<td><?php echo isset($params[2])?$params[2]:'';?></td>
			<td><?php echo isset($params[3])?$params[3]:'';?></td>
			<td><input type="text" class="val_<?php echo $id;?>"></td>
		</tr>
		<?php endforeach;?>
		<script>Wu.setData('<?php echo $id;?>','params','<?php echo json_encode($val['param']);?>');</script>
		<?php endif;?>
	</table>
</div>
<div>
	<p>请求方式：POST</p>
</div>
<div>
	<p><?php echo ($iii+1);?>.3返回结果</p>
	<div>
		<?php echo isset($val['return'])?$val['return']:'自己测试'?>
		<a href="javascript:;" onclick="Wu.run('<?php echo $id;?>','val_<?php echo $id;?>')">点击运行</a>
	</div>
	<script>
		Wu.setData('<?php echo $id;?>','url','<?php echo $url.$val['func_name'];?>');
		
		
	</script>
</div>
<hr>
<?php endforeach; ?>
</div>
<?php endforeach; ?>

<?php endforeach; ?>
</div>

<script>

Wu.init();
</script>
<body>
</html>

<?php




function init($path,$dirname=''){
	
	if(!is_dir($path)){
		return false;
	}

	$garr = array();

	if($handle = opendir($path)){
		while (false !== ($file = readdir($handle))){

			if($file=='.' || $file=='..'){
				continue;
			}
			
			$ls_file = $path . '/'.$file;
			
			
			

			if(!file_exists($ls_file)){
				
				continue;
			}
			
			if(!is_dir($ls_file)){
				$ls_arr = get_data($ls_file,$dirname);
				if(!$ls_arr){
					continue;
				}
				
				$garr[] = array(
					'name'=>$ls_arr['name'],
					'list'=>$ls_arr['list']
				);
			}
			else{
				$dname = basename($ls_file);
				$arr = init($ls_file,$dname);
				
				$garr = array_merge($garr,$arr);

			}

		}
	}
	closedir($handle);
	return $garr;
}


function get_data($file,$dirname){
	
	$ww = file_get_contents($file);	
	$contr = '';
	preg_match("/class (.*?)[ |\{]/is",$ww,$matchs1);
	if(count($matchs1)>=2){
		$contr = $matchs1[1];
	}

	
	
	
	$name = '';
	preg_match("/\/\/(.*?)\nclass/is",$ww,$matchs1);
	if(count($matchs1)>=2){
		$name = $matchs1[1];
	}
	if(empty($name)){
		return false;
	}
	
	
	preg_match_all("/\/\/start(.*?)\/\/end.*?function(.*?)\(/is",$ww,$matchs);
	
	if(count($matchs)<=2){
		return false;
	}
	
	$arr = array(
		'name'=>'@name',
		'return'=>'@return',
		'param'=>' @param'
	);
	
	$list = array();
	foreach($matchs[1] as $index=>$val){
		
		$func_name = $matchs[2][$index];
		
		$fg = explode("\n",$val);
		
		$data = array();
		
		
		$ddd = !empty($dirname)?$dirname.'/':'';
		
		$data['func_name'] =  $ddd . $contr .'/'.trim($func_name);
		foreach($fg as $v){
			if(empty($v)  || strpos($v,'/*')){
				continue;
			}
			if(strpos($v,'*/')){
				break;
			}
			
			$ls_tt = trim($v);
			
			foreach($arr as $key=>$info){
				if(strpos($ls_tt,$info)){
					$ls_fg = explode($info,$ls_tt);
					if(count($ls_fg)>1){
						if($key=='param'){
							$params = trim($ls_fg[1]);
							
							$fg_p = explode(' ',$params);
							
							$data[$key][] = $fg_p;
						}
						else{
							$data[$key] = $ls_fg[1];
						}
						
					}
				}
			}
		}
		if(!isset($data['func_name'])){
			continue;
		}
		$list[] = $data;
	}
	
	$ls_arr = array(
		'name'=>$name,
		'list'=>$list
	);
	
	return $ls_arr;
}


function get_module_list($dir){
	$files = array();


	if(@$handle = opendir($dir)) { //注意这里要加一个@，不然会有warning错误提示：）
		while(($file = readdir($handle)) !== false) {
			if($file != ".." && $file != ".") { //排除根目录；
				if(is_dir($dir."/".$file)) { //如果是子文件夹，就进行递归
					$mpath = $dir."/".$file.'/info.php';
					if(!file_exists($mpath)){
						continue;
					}
					$ls_arr = include($mpath);
					$ls_arr['path'] = $dir."/".$file;
					$ls_arr['module_name'] = $file;
					
					$files[] = $ls_arr;
				}
			}
		}
		closedir($handle);
	}
	return $files;
}